/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.put.dcs_modeler.wizards.newComputingObject;

import java.util.logging.Logger;
import javax.swing.JPanel;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectInformation;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.api.project.ui.OpenProjects;
import org.netbeans.spi.project.ui.templates.support.Templates;
import org.openide.WizardDescriptor;

public final class NewComputingObjectVisualPanel1 extends JPanel
{

    private Project selectedProject = null;
    public static Logger log = Logger.getLogger( NewComputingObjectVisualPanel1.class.getName() );
    
    /** Creates new form NewComputingObjectVisualPanel1 */
    public NewComputingObjectVisualPanel1() 
    {
        initComponents();
        completeProjectCombo();
        if ( this.selectedProject != null )
        {
            ProjectInformation pi = ProjectUtils.getInformation( selectedProject );
            setSelectedProject( pi.getName() );
            
        }
    }
    
    private void completeProjectCombo()
    {
        Project[] projects = OpenProjects.getDefault().getOpenProjects();
        for ( Project tmp : projects )
        {
            ProjectInformation pi = ProjectUtils.getInformation( tmp );
            projectCombo.addItem( new String( pi.getName() ) );
        }
    }
    
    private Project getSelectedProject()
    {
        Project[] projects = OpenProjects.getDefault().getOpenProjects();
        for ( Project tmp : projects )
        {
            ProjectInformation pi = ProjectUtils.getInformation( tmp );
            if ( pi.getName().equals( ( String )( projectCombo.getSelectedItem() ) ) )
            {
                return tmp;
            }
        }
        return null;
    }

    private void  setSelectedProject( String name )
    {
        projectCombo.setSelectedItem( name );
    }

    @Override
    public String getName() 
    {
        return "Project select";
    }
    
    public void storeSettings( WizardDescriptor d )
    {

        Project proj = getSelectedProject();

        d.putProperty( "project", proj );
        d.putProperty( "objectName", this.objectName.getText() );
    }
    
    public void readSettings( WizardDescriptor d )
    {
        this.selectedProject = ( Project )d.getProperty( "project" );
        setSelectedProject( this.selectedProject.getProjectDirectory().getName() );
        this.objectName.setText( ( String )d.getProperty( "objectName" ) );
        //log.info( Templates.getProject(d ).getProjectDirectory().getNameExt() );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jLabel1 = new javax.swing.JLabel();
        projectCombo = new javax.swing.JComboBox();
        objectName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel1.class, "NewComputingObjectVisualPanel1.jLabel1.text")); // NOI18N

        objectName.setText(org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel1.class, "NewComputingObjectVisualPanel1.objectName.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel1.class, "NewComputingObjectVisualPanel1.jLabel2.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(objectName, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
                    .addComponent(projectCombo, 0, 311, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(102, 102, 102)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(objectName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(projectCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(140, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField objectName;
    private javax.swing.JComboBox projectCombo;
    // End of variables declaration//GEN-END:variables
}

